<?php

namespace app\api\controller;

use app\api\logic\lawyer\SingleOrderLogic;
use app\api\validate\OrderValidate;
use app\common\controller\UserBase;
use app\common\logic\OrderLogic;
use app\common\model\ExpressOrder;
use app\common\model\Group;
use app\common\model\GroupOrder;
use app\common\model\Log;
use think\Exception;
use think\exception\ValidateException;
use think\facade\Db;

class Order extends UserBase
{
       // 验证下单
    public function add()
    {
        $data = (new OrderValidate())->doCheck();
        $data['user_id'] = $this->uid;
        $order= (new SingleOrderLogic())->add($data);
        $this->success(['order_id'=>$order['id']]);
    }
    
    // 订单列表
    public function getList()
    {
        $where = [];
        if (!empty($this->data['status'])) {
            $where[] = ['status',"=",$this->data['status']];
        }
        $list = \app\common\model\Order::where("user_id",$this->uid)
            ->where($where)
            ->where("status",">",0)
            ->order("id desc")
            ->paginate(['list_rows'=>$this->limit,"page"=>$this->page]);
        $this->success($list);
    }
    // 订单详情
    public function getDetail()
    {
        $obj = \app\common\model\Order::where("id",$this->data['id'])
            ->where('user_id',$this->uid)
            ->find();
        if (!$obj) $this->error('订单错误');
        $this->success($obj);
    }
    // 核销页面
    public function getCheck()
    {
        $obj = \app\common\model\Order::where("id",$this->data['id'])
            ->where('user_id',$this->uid)
            ->where('status',1)
            ->find();
        if (!$obj) $this->error('订单错误');
        $result['order_no'] = $obj->order_no;
        if ($obj->yifu > 0) $result["list"][] = ['type'=>"yifu","title"=>"衣服"];
        if ($obj->hua > 0) $result["list"][] = ['type'=>"hua","title"=>"鲜花"];
        if ($obj->quan > 0) $result["list"][] = ['type'=>"quan","title"=>"百元券"];
        $this->success($result);
    }
    // 核销
    public function doCheck()
    {
        try {
            $rule = [
                "order_no|单号" => "require",
                "type|类型" => "require|in:yifu,hua,quan"
            ];
            $this->validate($this->data,$rule);
            $obj = \app\common\model\Order::where("order_no",$this->data['order_no'])
                ->where('status',1)
                ->find();
            $type = $this->data['type'];
            if (!$obj) $this->error('订单错误');
            if ($obj->$type <= 0) $this->error('订单错误');

            // $log = Log::where("order_id",$obj['id'])
            //     ->where('type',$type)
            //     ->whereMonth('create_time')
            //     ->find();
            // if ($log) $this->error('本月已经核销过了');

            $obj->$type -- ;
            if ($obj->yifu == 0 && $obj->hua == 0 && $obj->quan == 0) {
                $obj->status == 2;
            }
            $obj->save();
            $content = "优惠券核销";
            if ($type == "yifu") $content = "衣服核销";
            if ($type == "hua") $content = "鲜花核销";
            Log::create([
                "order_id" => $obj['id'],
                "type" => $type,
                "goods_id" => $obj->goods_id,
                "content" => $content,
                "user_id" => $obj->user_id
            ]);
            $this->success();
        } catch (Exception | ValidateException $e) {
            $this->error($e->getMessage());
        }
    }
    // 核销记录
    public function logList()
    {
        $list = Log::where("user_id",$this->uid)
            ->order("id desc")
            ->field('id,content,create_time')
            ->paginate(['list_rows'=>$this->limit,"page"=>$this->page]);
        $this->success($list);
    }

    // 取消订单
    public function cancel()
    {
        $obj = \app\common\model\Order::where("id",$this->data['id'])
            ->where('user_id',$this->uid)
            ->find();
        if (!$obj) $this->error('订单不存在');
        if ($obj->status != -1) {
            $this->error('不能取消');
        }
        $obj->delete();
        $this->success();
    }
    
    // 确认完成
    public function confirm()
    {
        if (empty($this->data['id'])) throwError('id错误');
        $obj = \app\common\model\Order::where("id",$this->data['id'])
            ->where('user_id',$this->uid)
            ->with(['category'])
            ->find();
        if ($obj) {
            $obj->status = 3;
            $info = $obj->info;
            $info['finish_time'] = date("Y-m-d H:i");
            $obj->info = $info;
            $obj->save();
        }
        $this->success();
    }
    
    // 商城订单
    public function createOrder()
    {
        $order = OrderLogic::createOrder($this->data);
        $this->success($order);
    }

    public function orderPre()
    {
        $data = OrderLogic::orderPre($this->data);
        $this->success($data);
    }

    public function getOrderList()
    {
        $data = OrderLogic::getOrderList($this->data);
        $this->success($data);
    }

    public function delOrder()
    {
        OrderLogic::delOrder($this->data);
        $this->success();
    }

    public function setOrderStatus()
    {
        OrderLogic::setOrderStatus($this->data);
        $this->success();
    }

    public function getOrderDetail()
    {
        $order = OrderLogic::getOrderDetail($this->data);
        $this->success($order);
    }

    public function orderConfirm()
    {
        OrderLogic::setOrderStatus([
            "id" => $this->data['id'],
            "user_id" => $this->data['user_id'],
            "status" => 400
        ]);
        $this->success();
    }
    
    /**
     * @return void
     * author 小李子 2023-10-30 16:35
     * description 根据订单ID获取 条形码
     */
    public function getBarcode()
    {
        $order_id = input("order_id/d");
        if (empty($order_id)) throwError();
        OrderLogic::getBarcode($order_id,$this->uid);
    }
    
    // 通过pay_id 支付
    public function pay()
    {
        if (!isset($this->data['pay_id'])) $this->error();
        $this->data['openid'] = $this->user->openid;
        $this->data['user_id'] = $this->uid;
        $this->success(OrderLogic::createPay($this->data));
    }

    // 通过订单返回pay_id
    public function orderPay()
    {
        if (!isset($this->data['order_id'])) $this->error();
        if (isset($this->data['bz'])) {
            $order = \app\common\model\Order::where("user_id",$this->uid)
                ->find($this->data['order_id']);
            if (!$order) throwError('订单不存在');
            $order->bz = $this->data['bz'];
            $order->save();
        }
        $pay_id = \app\common\logic\common\PayLogic::createOrUpdatePayOrder($this->data['order_id']);
        $this->success(['pay_id'=>$pay_id]);
    }

    // 订单评价
    public function orderComment()
    {
        OrderLogic::orderComment($this->data);
        $this->success();
    }

    // 退款
    public function orderReturned()
    {
        OrderLogic::orderReturned($this->data);
        $this->success();
    }
}